package app.project.directionlensproject.serverapi;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.Context;
import android.util.Log;
import app.project.directionlensproject.model.Category;
import app.project.directionlensproject.model.Place;
import app.project.directionlensproject.util.CommonUtils;
import app.project.directionlensproject.util.Constants;

public class ServerAPI {
	static final String NAMESPACE = "http://tempuri.org/";
	static final String URL = "http://192.168.1.101/Lens/Lens.asmx";

	public static ArrayList<Place> GetNearbyPlace(int categoryType, double locationx, double locationy, int radius,
			int offSet, int limit) {
		String jsonResult;
		jsonResult = GetNearbyPlaceStr(categoryType, locationx, locationy, radius, offSet, limit);
		//Log.e("JsonResult",jsonResult);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetAllPlaceUnapproved(String tokenid, int offSet, int limit) {
		String jsonResult = GetAllPlaceUnapprovedStr(tokenid, offSet, limit);
		Log.e("JsonUnapproved",jsonResult);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetPlaceUnapproved(String tokenid, double locationx, double locationy, int radius,
			int offSet, int limit) {
		String jsonResult = GetPlaceUserStr(tokenid, locationx, locationy, radius, offSet, limit, false);
		Log.e("JsonUnApproved11111",jsonResult);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetAllPlaceApproved(String tokenid, int offSet, int limit) {
		String jsonResult = GetAllPlaceApprovedStr(tokenid, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetPlaceApproved(String tokenid, double locationx, double locationy, int radius,
			int offSet, int limit) {
		String jsonResult = GetPlaceUserStr(tokenid, locationx, locationy, radius, offSet, limit, true);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetAllUserFavouritePlaces(String tokenid, int offSet, int limit) {
		String jsonResult = GetAllUserFavouritePlacesStr(tokenid, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetUserFavouritePlaces(String tokenid, double locationx, double locationy,
			int radius, int offSet, int limit) {
		String jsonResult = GetUserFavouritePlacesStr(tokenid, locationx, locationy, radius, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetSystemFavouritePlaces(String tokenid, double locationx, double locationy,
			int radius, int offSet, int limit) {
		String jsonResult = GetSystemFavouritePlacesStr(tokenid, locationx, locationy, radius, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> GetAllSystemFavouritePlaces(String tokenid, int offSet, int limit) {
		String jsonResult = GetAllSystemFavouritePlacesStr(tokenid, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static boolean registerUser(Context context, String username, String password, String email) {
		final String METHOD_NAME = "RegisterUser";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("username");
		pi.setValue(username);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("password");
		pi2.setValue(password);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("email");
		pi3.setValue(email);
		pi3.setType(String.class);
		request.addProperty(pi3);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			JSONObject jsonObject = new JSONObject(responseJson);
			boolean result = Boolean.parseBoolean(jsonObject.getString("Result"));
			if (result) {
				String token = jsonObject.getString("tokenid");
				CommonUtils.saveStringSetting(context, Constants.TOKEN_ID, token);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean login(Context context, String username, String password) throws Exception {
		final String METHOD_NAME = "UserLogin";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("username");
		pi.setValue(username);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("password");
		pi2.setValue(password);
		pi2.setType(String.class);
		request.addProperty(pi2);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
		SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
		String responseJson = response.toString();
		JSONObject jsonObject = new JSONObject(responseJson);
		boolean result = Boolean.parseBoolean(jsonObject.getString("Result"));
		if (result) {
			String token = jsonObject.getString("tokenid");
			CommonUtils.saveStringSetting(context, Constants.TOKEN_ID, token);
		}
		return result;
	}

	public static boolean ChangePassword(String tokenid, String username, String oldPassword, String newPassword) {
		final String METHOD_NAME = "ChangePassword";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("username");
		pi.setValue(username);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("oldPassword");
		pi3.setValue(oldPassword);
		pi3.setType(String.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("newPassword");
		pi4.setValue(newPassword);
		pi4.setType(String.class);
		request.addProperty(pi4);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean RemoveUserFavouritePlaces(String tokenid, String uniqueid) {
		final String METHOD_NAME = "RemoveUserFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("tokenid");
		pi.setValue(tokenid);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("uniqueid");
		pi2.setValue(uniqueid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean SetSystemFavouritePlaces(String tokenid, String uniqueid) {
		final String METHOD_NAME = "SetSystemFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("tokenid");
		pi.setValue(tokenid);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("uniqueid");
		pi2.setValue(uniqueid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean SetUserFavouritePlaces(String tokenid, String uniqueid) {
		final String METHOD_NAME = "SetUserFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("tokenid");
		pi.setValue(tokenid);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("uniqueid");
		pi2.setValue(uniqueid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean RemoveUserPlaces(String tokenid, String uniqueid) {
		final String METHOD_NAME = "RemoveUserPlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("tokenid");
		pi.setValue(tokenid);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("uniqueid");
		pi2.setValue(uniqueid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean EditUserPlaces(String tokenid, String uniqueid, double locationx, double locationy,
			String placename, String description, String image, int categoryType) {
		final String METHOD_NAME = "EditUserPlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("tokenid");
		pi21.setValue(tokenid);
		pi21.setType(String.class);
		request.addProperty(pi21);

		PropertyInfo pi22 = new PropertyInfo();
		pi22.setName("uniqueid");
		pi22.setValue(uniqueid);
		pi22.setType(String.class);
		request.addProperty(pi22);

		PropertyInfo pi23 = new PropertyInfo();
		pi23.setName("placename");
		pi23.setValue(placename);
		pi23.setType(String.class);
		request.addProperty(pi23);

		PropertyInfo pi24 = new PropertyInfo();
		pi24.setName("description");
		pi24.setValue(description);
		pi24.setType(String.class);
		request.addProperty(pi24);

		PropertyInfo pi25 = new PropertyInfo();
		pi25.setName("image");
		pi25.setValue(image);
		pi25.setType(String.class);
		request.addProperty(pi25);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("locationx");
		pi2.setValue(locationx);
		pi2.setType(double.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("categoryType");
		pi4.setValue(categoryType);
		pi4.setType(int.class);
		request.addProperty(pi4);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static String SetUserPlaces(String tokenid, double locationx, double locationy, String placename,
			String description, String image, int categoryType) {
		final String METHOD_NAME = "SetUserPlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("tokenid");
		pi21.setValue(tokenid);
		pi21.setType(String.class);
		request.addProperty(pi21);

		PropertyInfo pi23 = new PropertyInfo();
		pi23.setName("placename");
		pi23.setValue(placename);
		pi23.setType(String.class);
		request.addProperty(pi23);

		PropertyInfo pi24 = new PropertyInfo();
		pi24.setName("description");
		pi24.setValue(description);
		pi24.setType(String.class);
		request.addProperty(pi24);

		PropertyInfo pi25 = new PropertyInfo();
		pi25.setName("image");
		pi25.setValue(image);
		pi25.setType(String.class);
		request.addProperty(pi25);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("locationx");
		pi2.setValue(locationx);
		pi2.setType(double.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("categoryType");
		pi4.setValue(categoryType);
		pi4.setType(int.class);
		request.addProperty(pi4);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			return responseJson;
			//Log.e("responseJson",responseJson);
			//boolean result = Boolean.parseBoolean(responseJson);
			//return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static ArrayList<Place> SearchAllPlace(String tokenid, String placename, int offSet, int limit) {
		String jsonResult = SearchAllPlaceStr(tokenid, placename, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static ArrayList<Place> SearchPlace(String tokenid, String placename, double locationx, double locationy,
			int radius, int offSet, int limit) {
		String jsonResult = SearchPlaceStr(tokenid, placename, locationx, locationy, radius, offSet, limit);
		return GetAllPlaceFromJson(jsonResult);
	}

	public static boolean RequestPasswordReset(String emailAddress) {
		final String METHOD_NAME = "RequestPasswordReset";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("emailAddress");
		pi.setValue(emailAddress);
		pi.setType(String.class);
		request.addProperty(pi);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean ResetPassword(String emailAddress, String verifyCode) {
		final String METHOD_NAME = "ResetPassword";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi = new PropertyInfo();
		pi.setName("emailAddress");
		pi.setValue(emailAddress);
		pi.setType(String.class);
		request.addProperty(pi);

		PropertyInfo pi1 = new PropertyInfo();
		pi1.setName("verifyCode");
		pi1.setValue(verifyCode);
		pi1.setType(String.class);
		request.addProperty(pi1);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			boolean result = Boolean.parseBoolean(responseJson);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static ArrayList<Category> GetAllCatogery(){
		String jsonResult = GetAllCatogeryStr();
		if (jsonResult == null)
			return null;
		try {
			ArrayList<Category> listCate = new ArrayList<Category>();
			JSONArray arrayPlace = new JSONArray(jsonResult);
			int length = arrayPlace.length();
			for (int i = 0; i < length; i++) {
				JSONObject object = arrayPlace.getJSONObject(i);
				Category tmp = new Category();
				tmp.categoryId = Integer.parseInt(object.getString("CategoryId").trim());
				tmp.image = object.getString("CategoryImage");
				tmp.name = object.getString("CategoryName");
				listCate.add(tmp);
			}

			return listCate;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<Category>();
	}
	
	private static String GetAllCatogeryStr() {
		final String METHOD_NAME = "GetAllCatogery";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			String responseJson = response.toString();
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static String SearchAllPlaceStr(String tokenid, String placename, int offSet, int limit) {
		String METHOD_NAME = "SearchAllPlace";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("tokenid");
		pi21.setValue(tokenid);
		pi21.setType(String.class);
		request.addProperty(pi21);

		PropertyInfo pi22 = new PropertyInfo();
		pi22.setName("placename");
		pi22.setValue(placename);
		pi22.setType(String.class);
		request.addProperty(pi22);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String SearchPlaceStr(String tokenid, String placename, double locationx, double locationy,
			int radius, int offSet, int limit) {
		String METHOD_NAME = "SearchPlace";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("tokenid");
		pi21.setValue(tokenid);
		pi21.setType(String.class);
		request.addProperty(pi21);

		PropertyInfo pi22 = new PropertyInfo();
		pi22.setName("placename");
		pi22.setValue(placename);
		pi22.setType(String.class);
		request.addProperty(pi22);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("locationx");
		pi2.setValue(locationx);
		pi2.setType(double.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("radius");
		pi4.setValue(radius);
		pi4.setType(int.class);
		request.addProperty(pi4);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetAllSystemFavouritePlacesStr(String tokenid, int offSet, int limit) {
		String METHOD_NAME = "GetAllSystemFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetUserFavouritePlacesStr(String tokenid, double locationx, double locationy, int radius,
			int offSet, int limit) {
		String METHOD_NAME = "GetUserFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("locationx");
		pi21.setValue(locationx);
		pi21.setType(double.class);
		request.addProperty(pi21);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("radius");
		pi4.setValue(radius);
		pi4.setType(int.class);
		request.addProperty(pi4);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetSystemFavouritePlacesStr(String tokenid, double locationx, double locationy, int radius,
			int offSet, int limit) {
		String METHOD_NAME = "GetSystemFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("locationx");
		pi21.setValue(locationx);
		pi21.setType(double.class);
		request.addProperty(pi21);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("radius");
		pi4.setValue(radius);
		pi4.setType(int.class);
		request.addProperty(pi4);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetAllUserFavouritePlacesStr(String tokenid, int offSet, int limit) {
		String METHOD_NAME = "GetAllUserFavouritePlaces";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetAllPlaceApprovedStr(String tokenid, int offSet, int limit) {
		String METHOD_NAME = "GetAllPlaceApproved";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetPlaceUserStr(String tokenid, double locationx, double locationy, int radius, int offSet,
			int limit, boolean isApprove) {
		String METHOD_NAME = "GetPlaceApppproved";
		if (!isApprove) {
			METHOD_NAME = "GetPlaceUnapproved";
		}
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi21 = new PropertyInfo();
		pi21.setName("tokenid");
		pi21.setValue(tokenid);
		pi21.setType(String.class);
		request.addProperty(pi21);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("locationx");
		pi2.setValue(locationx);
		pi2.setType(double.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("radius");
		pi4.setValue(radius);
		pi4.setType(int.class);
		request.addProperty(pi4);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetAllPlaceUnapprovedStr(String tokenid, int offSet, int limit) {
		String METHOD_NAME = "GetAllPlaceUnapproved";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("tokenid");
		pi2.setValue(tokenid);
		pi2.setType(String.class);
		request.addProperty(pi2);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String GetNearbyPlaceStr(int categoryType, double locationx, double locationy, int radius,
			int offSet, int limit) {
		String METHOD_NAME = "GetNearbyPlace";
		if (categoryType < 0) {// get all
			METHOD_NAME = "GetAllNearbyPlace";
		}
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		if (categoryType >= 0) {
			PropertyInfo pi = new PropertyInfo();
			pi.setName("categoryType");
			pi.setValue(categoryType);
			pi.setType(int.class);
			request.addProperty(pi);
		}

		PropertyInfo pi2 = new PropertyInfo();
		pi2.setName("locationx");
		pi2.setValue(locationx);
		pi2.setType(double.class);
		request.addProperty(pi2);

		PropertyInfo pi3 = new PropertyInfo();
		pi3.setName("locationy");
		pi3.setValue(locationy);
		pi3.setType(double.class);
		request.addProperty(pi3);

		PropertyInfo pi4 = new PropertyInfo();
		pi4.setName("radius");
		pi4.setValue(radius);
		pi4.setType(int.class);
		request.addProperty(pi4);

		PropertyInfo pi5 = new PropertyInfo();
		pi5.setName("offSet");
		pi5.setValue(offSet);
		pi5.setType(int.class);
		request.addProperty(pi5);

		PropertyInfo pi6 = new PropertyInfo();
		pi6.setName("limit");
		pi6.setValue(limit);
		pi6.setType(int.class);
		request.addProperty(pi6);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.implicitTypes = true;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;
		envelope.setOutputSoapObject(request);
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service Method
			Object responseObject = envelope.getResponse();
			if (responseObject instanceof SoapObject) {
				SoapObject response = (SoapObject) responseObject;
				return response.toString();
			} else {
				SoapPrimitive response = (SoapPrimitive) responseObject;
				return response.toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static ArrayList<Place> GetAllPlaceFromJson(String jsonResult) {
		if (jsonResult == null)
			return null;
		if (jsonResult.contains("anyType{}"))
			return new ArrayList<Place>();

		try {
			ArrayList<Place> listPlaces = new ArrayList<Place>();
			JSONArray arrayPlace = new JSONArray(jsonResult);
			int length = arrayPlace.length();
			for (int i = 0; i < length - 1; i++) {
				JSONObject object = arrayPlace.getJSONObject(i);
				Place tmp = new Place();
				tmp.categoryid = Integer.parseInt(object.getString("CategoryId").trim());
				tmp.created_at = object.getString("CreatedAt");
				tmp.description = object.getString("Description");
				tmp.image = object.getString("Image");
				tmp.locationx = Float.parseFloat(object.getString("LocationX").trim());
				tmp.locationy = Float.parseFloat(object.getString("LocationY").trim());
				tmp.placename = object.getString("PlaceName");
				tmp.uniqueid = object.getString("UniqueId").trim();
				tmp.updated_at = object.getString("UpdatedAt");
				listPlaces.add(tmp);
			}

			return listPlaces;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<Place>();
	}
}
